<?php
/** Une classe représentant les saisons
 * Auteur : Sebastian Mosser
 */

class Season implements iPersistantData, iHTMLViewable {
	
	private $_id;
	private $_begin;
	private $_end;
	
	public static $NO_SEASON = -1;
	public static $NO_SEASON_DISPLAY = "Pas de saison choisie";
	
	public function __construct(Date $b,Date $e, $id = -1){
		$this->_begin = $b;
		$this->_end = $e;
		$this->_id = $id;
	}
	
	/** Accesseurs **/
	public function getBegin() { return $this->_begin; }
	public function getEnd() { return $this->_end; }
	
	/** Implementation de l'interface iHTMLViewable **/
	
	public function displayLong() {
		return $this->_begin->prettyPrint() . " --> " . $this->_end->prettyPrint();
	}
	
	public function displayShort() {
		return $this->_begin->getYear() . " - " . $this->_end->getYear();
	}
	
	public function displayInline($scriptUrl="") {
		return "<tr><td>$this->_begin->sqlFormat()</td><td>$this->_begin->sqlFormat()</td></tr>";
	}

	
	/** Fin de l'implementation de l'interface iHTMLViewable **/
	
	/** Implementation de l'interface iPersistantData**/
	// Creation de la table persistante
	public static function createTable() {
		$query  = " CREATE TABLE IF NOT EXISTS `season` ( \n";
		$query .= "  `id`    INTEGER     NOT NULL PRIMARY KEY auto_increment, \n";
		$query .= "  `begin` VARCHAR(10) NOT NULL, \n";
		$query .= "  `end`   VARCHAR(10) NOT NULL);\n";
		
		$myDb = Database::getSingleton();
		$myDb->execute($query);
	
	}
	// Supression de la table persistante
	public static function dropTable() {
	
		$myDb = Database::getSingleton();
		$myDb->execute("DROP TABLE IF EXISTS `season`");
	
	}

	// Creation de la donnée persistante
	public function create() {
		
		if ($this->_id != -1)
			throw new RuntimeException("Season/create()",
												"this object is still present in database !");
												
		$begin = Database::escape($this->_begin->sqlFormat());
		$end   = Database::escape($this->_end->sqlFormat());
		$sql = "INSERT INTO `season` VALUES ('', '$begin', '$end')";

		$myDb = Database::getSingleton();
		/**
		$myDb->execute($sql);
		($myDb->lastKeyValue() != 0 ? $this->_id = $myDb->lastKeyValue() : $this->_id = -1);
		**/
		$result = $myDb->executeInsert($sql);
		if ($result[0] != 0)
			$this->_id = $result[0];
		else
			$this->id = -1;
	}
	
	// Lecture d'un element persistant
	public static function read($key) {
		$key = Database::escape($key);
		$sql = "SELECT * FROM `season` WHERE `id` = " .$key;
		
		$myDb = Database::getSingleton();
		$ans = mysql_fetch_assoc($myDb->execute($sql));
		
		if (! $ans)
			throw new RuntimeException("Season/read()","Unknown key '$key'");
		
		return new Season(Date::fromSql($ans["begin"]),
								Date::fromSql($ans["end"]),
								$ans["id"]);
		
	}
	// Mise a jour d'un element
	public function update(){
		
		if ($this->_id == -1)
			throw new RuntimeException("Season/create()",
												"this object wasn't saved in database !");
		
		$begin = Database::escape($this->_begin->sqlFormat());
		$end   = Database::escape($this->_end->sqlFormat());
		$sql   = "UPDATE `season` SET \n";
		$sql  .= "  `begin` = '$begin', \n";
		$sql  .= "  `end`   = '$end'    \n";
		$sql  .= "  WHERE `id` = $this->_id";
		
		$myDb = Database::getSingleton();
		$myDb->execute($sql);
	
	}
	// supression d'un element
	public function delete() {
		if ($this->_id == -1)
			throw new RuntimeException("Season/create()",
												"this object wasn't saved in database !");
		$sql = "DELETE FROM `season` WHERE `id` = $this->_id";
		
		$myDb = Database::getSingleton();
		$myDb->execute($sql);
	
	}
	
	public function tag() {
	 		$tmp = array();
	 		$tmp[0] = "Season";
	 		$tmp[1] = $this->_id;
	 		return $tmp;
	}	
	
	public function soundsLike($o) {
	 		return ($this->_begin == $o->_begin && $this->_end == $o->_end);
	 	}
	
	/** Fin de l'implementation de la persistance des données **/
	
	
	public static function getLastSeason() {
		$q = "SELECT `id` FROM `season` ORDER BY `id` DESC;";
		$myDb = Database::getSingleton();
		$ans = Database::assoc($myDb->execute($q));
		return $ans["id"];
	}
	
	public static function titleBar($s) {
		
		if ($s == self::$NO_SEASON)
			return "[ " . self::$NO_SEASON_DISPLAY." ]";
		
		$mySeason = self::read($s);
		return "[ " . $mySeason->displayShort() . " ]";
	
	}
	
	
	public static function getAllSeasonId() {
		$q = "SELECT `id` FROM `season` ORDER BY `id` DESC;";
		$myDb = Database::getSingleton();
		$ans = $myDb->execute($q);
		
		$result = array();
		while (($row = Database::assoc($ans)) != False) {
			$result[] = $row["id"];
		}
		return $result;
	
	}
	
	public static function getAsList($id) {
		$ans = self::getAllSeasonId();
		$result = "";
		foreach($ans as $k => $v) {
			$aSeason = self::read($v);
			if ($v == $id)
				$result .= "<option value=\"$v\" selected>".$aSeason->displayShort(). "</option>";
			else 
				$result .= "<option value=\"$v\">".$aSeason->displayShort(). "</option>";
		}
		return $result;
	}
	
	
}
 
 
?>